﻿package {
	import gs.TweenLite;
	import fl.controls.ColorPicker;
    import fl.events.ColorPickerEvent;
	import flash.display.BitmapData;
	import flash.display.*;
	import flash.display.Loader;
    import flash.net.URLRequest;
	import org.papervision3d.materials.shadematerials.EnvMapMaterial;
	import org.papervision3d.materials.shadematerials.CellMaterial;

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import org.papervision3d.core.math.*;
	//import viewportlayer for dae
	 import org.papervision3d.view.layer.ViewportLayer;
	 import org.papervision3d.view.layer.util.ViewportLayerSortMode;
	//set line3d
	import org.papervision3d.materials.BitmapMaterial;
	  import org.papervision3d.materials.special.LineMaterial;
      import org.papervision3d.core.geom.renderables.Vertex3D;
	  import org.papervision3d.core.geom.renderables.Line3D;
	  import org.papervision3d.core.geom.Lines3D;
		
    	 import org.papervision3d.lights.PointLight3D;
		 import org.papervision3d.materials.WireframeMaterial;
		 import org.papervision3d.materials.ColorMaterial;
		import org.papervision3d.materials.utils.MaterialsList;
		import org.papervision3d.core.proto.*;
    import org.papervision3d.cameras.Camera3D;
    import org.papervision3d.events.FileLoadEvent;
    import org.papervision3d.materials.BitmapFileMaterial;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;
    import org.papervision3d.events.InteractiveScene3DEvent;
    import org.papervision3d.objects.parsers.*;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.special.CompositeMaterial;
	 import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.materials.shadematerials.PhongMaterial;
	import org.papervision3d.materials.shadematerials.GouraudMaterial
     import org.papervision3d.core.proto.LightObject3D;
	 import org.papervision3d.objects.DisplayObject3D;
    public class Papervision extends Sprite {
        protected var materialList:MaterialsList;
         private var object    :DAE;
        public var panorama:Viewport3D = new Viewport3D(750,450);
        public var scena:Scene3D = new Scene3D();
        public var kamera:Camera3D = new Camera3D();
		public var vpCustom:Viewport3D = new Viewport3D(450,450);
        public var sCustom:Scene3D = new Scene3D();
        public var cCustom:Camera3D = new Camera3D();
        public var ravan:Plane;
        public var materijal:BitmapFileMaterial;
        public var renderer:BasicRenderEngine = new BasicRenderEngine();
		 private var _radius:Number = 50;
  		private var _light:PointLight3D;
		private var selectedObject:DisplayObject3D;
		private var prevMouseX:Number = 0;
        private var prevMouseY:Number = 0;
		public var lastMousePoint : Point = new Point();
		public var isMouseDown : Boolean = false; 
		public var isMousePress : Boolean = false; 
		public var pitch : Number = 0; 
		public var targetPitch : Number = 0; 
		public var yaw : Number = 0; 
		public var targetYaw : Number = 0;
		//set viewportlayer for dae
		private var layer     :ViewportLayer;
		protected var viewportLayer:ViewportLayer;
		protected var viewportLayer2:ViewportLayer;
		//set movex and y
		protected var initialTransform:Matrix3D;
        protected var initialMouseX:Number;
        protected var initialMouseY:Number;
		//set for childs in DAE:
		private var dae1:DisplayObject3D;
		private var dae2:DisplayObject3D;
		private var dae3:DisplayObject3D;
		private var dae4:DisplayObject3D;
		private var dae5:DisplayObject3D;
		private var dae6:DisplayObject3D;
		private var dae7:DisplayObject3D;
		private var dae8:DisplayObject3D;
		private var dae9:DisplayObject3D;
		private var dae10:DisplayObject3D;
		private var maxX :Number = 0;
		private var minX :Number = 0;
		private var maxY :Number = 0;
		private var minY :Number = 0;
		private var maxZ :Number = 0;
		private var minZ :Number = 0;
		//remove dae
		private var c_parent:DisplayObjectContainer3D;
		//creat a light in scene
		private var light:PointLight3D = new PointLight3D(true);
		private var colorPicker:ColorPicker = new ColorPicker();
		//set array list for DAE custom
		private var DaeArray:Array = new Array();
		private var subject:DisplayObject3D;
		//set toa do khi drag ra khoi khung.
		private var indexOfCar:Number3D
		//set position for custom DAE
		public var newPoint : Point = new Point();
		public static var nameObject:String;
		public function setCustomvp(evt:Event=null) : void
		{
			//selectedObject = null;
			//selectedObject = new DAE(); 
			subject= new DAE();
			trace("sdksjfksfk"+selectedObject);
			//subject.load(selectedObject".dae");
     	 	//selectedObject.load("");
			addChild(vpCustom);
			vpCustom.x=500;
			vpCustom.y=200;
			addEventListener(Event.ENTER_FRAME,loadingDAE);
		}
		public function loadingDAE(evt:Event):void{
			renderer.renderScene(sCustom,cCustom,vpCustom);
		}
		public function mouseUp(e : MouseEvent) : void
		{
			isMouseDown = false; 
			panorama.stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown); 
			//kamera= new InteractiveCamera3D(panorama);
			
		}
		public function moveEvt(e : MouseEvent) : void
		{	
			removeEventListener(Event.ENTER_FRAME, gameLoop); 
			 if ( e.altKey ) {
				startInteraction(object);
            }
			
			
		}
		public function mouseDown(e : MouseEvent) : void
		{
			
			isMouseDown = true; 
			
			lastMousePoint.x = mouseX; 
			lastMousePoint.y = mouseY; 
			
		}
		public function updateCamera() : void
		{
			
			if(isMouseDown)
			{
				targetPitch += ((mouseY - lastMousePoint.y)*0.4);
				targetYaw += ((mouseX - lastMousePoint.x)*0.4);
				
				lastMousePoint.x = mouseX; 
				lastMousePoint.y = mouseY; 
			}
			
			pitch +=((targetPitch-pitch)*0.3); 
			yaw +=((targetYaw-yaw)*0.3); 
			
			
			kamera.x = 0;
			kamera.y = 100;
			kamera.z = 0;
			
			kamera.rotationX = pitch;
			kamera.rotationY = yaw; 
			
			kamera.moveBackward(1000);
		}
		
		private function gameLoop(event:Event):void 
		{
			panorama.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown); 
			panorama.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
			updateCamera();
			

		}
		
		public function viewPortMouseOut( event:MouseEvent )
			  {
					trace("mouseup");
					//kamera= new InteractiveCamera3D(panorama);
			  }
		public function viewPortMouseOver( event:MouseEvent )
			  {
					
			  }
		public function OriginalState( event:Event=null ):void { 
				kamera= new InteractiveCamera3D(panorama);
				updateCamera();
			  }
		public function addedToStage( event:Event)
			  {
				//kamera= new InteractiveCamera3D(panorama);
				updateCamera();
				
				
			  }
		//set rotation for dae
		 public function rotationDAE():void 

			{ 
				addEventListener(Event.ENTER_FRAME, gameLoop); 
			}
		 protected function stopInteraction( e:Event = null ):void {
           	 panorama.stage.removeEventListener( Event.ENTER_FRAME, moveHandler );
           	 panorama.stage.removeEventListener( MouseEvent.MOUSE_UP, stopInteraction );
			//addEventListener(Event.ENTER_FRAME, gameLoop); 
           // _state = INACTIVE;
        }
		//set movex and y
		 protected function startInteraction(object:DisplayObject3D):void {
           // initialTransform = Matrix3D.clone( object.transform );
            //initialMouseX = panorama.stage.mouseX;
//            initialMouseY = panorama.stage.mouseY;
//            
//            panorama.stage.addEventListener( MouseEvent.MOUSE_UP, stopInteraction );
        }
		 protected function moveHandler( e:Event ):void {
           var x:Number = initialMouseX - panorama.stage.mouseX;
            var y:Number = initialMouseY - panorama.stage.mouseY;
			 initialMouseX = panorama.stage.mouseX;
          	 initialMouseY = panorama.stage.mouseY;
		   if(selectedObject.name=="khung"||selectedObject.name=="default_material"||selectedObject.name=="default_matden"){
			 dae1.x=dae9.x=dae10.x=selectedObject.x+=-x/50; 
			}
		   else selectedObject.x+=-x/50; 
			
            //object.copyTransform( initialTransform );
            //object.moveDown(y);
 				
						
        }
		//set index event

		
		public function removeZoom(event:Event=null) 
        {	
			kamera = new Camera3D();
			updateCamera();
			
		}
		public function moveAxis(event:Event=null) 
        {	
			panorama.stage.addEventListener(MouseEvent.MOUSE_DOWN, moveEvt);
			
			
		}
		private function updateMaterials():void
        {	
			var mc:GouraudMaterial = new GouraudMaterial(light,colorPicker.selectedColor,0x000000);
			// replace material and update events for dae
			mc.interactive=true;
			//mc.doubleSided =true;
			//mc.smooth = true;
			var mc2:GouraudMaterial = new GouraudMaterial(light,0x0000ff,0x000000);
			var mc7:GouraudMaterial = new GouraudMaterial(light,0x000000,0x000000);
			//img.contentLoaderInfo.addEventListener(Event.COMPLETE, loaddae);
			//var cmat:BitmapMaterial= new BitmapMaterial("MitLancerMap.jpg",true);
			//dae5.material=mc;
			var phong:PhongMaterial = new PhongMaterial(light, 0xFF0000, 0x000000,0);
			light.y = 50;
			light.z = 10;
			
			//addEventListener(Event.ENTER_FRAME,fr);
			var cellMat:CellMaterial = new CellMaterial(light, 0xFAAAAAA, 0xFFFFFF, 15);
			var flatShadeMat:FlatShadeMaterial = new FlatShadeMaterial(light, 0xff0000,0x000000);
			//dae5.replaceMaterialByName(mc,"Change-material");
			//dae5.replaceMaterialByName(mc,"change_dae5-material");
			 var colorMaterial:ColorMaterial = new ColorMaterial(0x0000ff);
			 var matList:MaterialsList =  new MaterialsList();
			 matList.addMaterial( colorMaterial,"change_dae5-material");
			 object.setChildMaterial(dae1,mc);
			 object.setChildMaterial(dae2,mc);
			 object.setChildMaterial(dae3,mc);
			 object.setChildMaterial(dae4,mc);
     	}
		private function changeHandler(event:ColorPickerEvent):void {
            updateMaterials();
        }
		public function changeColor():void{
				
				colorPicker.scaleX=.5;
				colorPicker.scaleY=.5;
           		addChild(colorPicker);
				colorPicker.visible=false;
				colorPicker.x = 270;
				colorPicker.selectedColor = 0x00ff00;
				colorPicker.open();
                colorPicker.addEventListener(ColorPickerEvent.CHANGE, changeHandler);

			//mainViewport.opaqueBackground = 0xffffff;
			//addChild(mainViewport);
				//on load for model option
				//var onLoad:
			}
        public function Papervision(url:String) 
        {	addChild(panorama);
			addEventListener(Event.ADDED_TO_STAGE,addedToStage);
			//this.addEventListener(MouseEvent.MOUSE_OUT,viewPortMouseOut );
			
 			 //add listener to overall stage mouse movement
 			 //this.addEventListener( MouseEvent.MOUSE_OVER,viewPortMouseOver );
			//set event for rotation two target...
			
			
            //panorama.filters = [new GlowFilter(0xcc0000, 1, 10, 10, 2)];
			panorama.opaqueBackground = 0x000000;
			panorama.interactive=true;
            materijal = new BitmapFileMaterial("MitLancerMap.jpg",true);
		   // materijal.tiled = true;
		    materijal.smooth = true;
			var mc3:GouraudMaterial = new GouraudMaterial(light,0xff0000,0x000000);
		    var colorMaterial:ColorMaterial = new ColorMaterial(0x0000ff);
			var colorMaterial2:ColorMaterial = new ColorMaterial(0x000000);
			var matList2:MaterialsList =  new MaterialsList();
			var matList:MaterialsList =  new MaterialsList();
			matList.addMaterial( colorMaterial,"change_dae5-material");
			matList.addMaterial( colorMaterial2,"_1_-_Default-material");
			//matList.addMaterial( colorMaterial,"_2_-_Default-material");
			//matList.addMaterial( colorMaterial,"_3_-_Default-material");
			
		   _light = new PointLight3D(false);
		
			//Create 3D Objects
			var color :uint = Math.random() * 0xFFFFFF;
			var mat:WireframeMaterial = new WireframeMaterial(color,.5);
			var colorM:ColorMaterial = new ColorMaterial(color);
			var plane:Plane = new Plane(colorM,1000,1000,5,5);
			scena.addChild(plane);
			plane.rotationX=90;
			plane.x=-1;
			
		  //set viewportlayer for dae
		  layer          = new ViewportLayer(panorama, null);
      	  //layer.sortMode = "index";
     	  panorama.containerSprite.addLayer(layer);
		  layer.sortMode = ViewportLayerSortMode.INDEX_SORT;
		  
		 
		 //set dk for dae
		 object       = new DAE(); 

     	 object.load(url,matList);
		 object.scale = 100;
         addEventListener(Event.ENTER_FRAME, render);
         scena.addChild(object);
		 // scena.addChild(plane);
			//setInteractiveMaterials(object,true);
		 object.addEventListener(FileLoadEvent.LOAD_COMPLETE, colladaLoaded);
          // object.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, clicker,false,0,true);
  
        }
		public function removeEnterFrame():void{
			removeEventListener(Event.ENTER_FRAME, render);
			}
		private function parseMesh(obj:DisplayObject3D):void
		{

			var i:int=0;
			for each(var vertex:Vertex3D in obj.geometry.vertices)
		{
			i++;
			var vx:Number = vertex.x;
			var vy:Number = vertex.y;
			var vz:Number = vertex.z;
			
			maxX += vx;
			maxY += vy;
			maxZ += vz;

		}
		//var vertex : Number3D = new Number3D (maxX/i,maxY/i,maxZ/i);
		trace("trace "+maxX/i);
		trace("trace "+maxY/i);
		trace("trace "+maxZ/i);
		obj.x =-maxX/i;
		obj.y= maxY/i;
		obj.z = maxZ/i;
		
		}
		public function cancelDAE():void{
				addEventListener(Event.ENTER_FRAME,render);
				if(nameObject=="khung"){
					//dae1= CustomVp.dae1;
					//dae9= CustomVp.dae3;
					//dae10 = CustomVp.dae4;
					
					//set for cancel
					dae1.x=newPoint.x;
					dae1.y=newPoint.y;
					TweenLite.to(dae1,1.5, { x:0, y:0, z:0});
					
					dae9.x=newPoint.x;
					dae9.y=newPoint.y;
					TweenLite.to(dae9,1.5, { x:0, y:0, z:0});
					//TweenLite.to(dae9,2.5, { x:0, y:0});
					
					dae10.x=newPoint.x;
					dae10.y=newPoint.y;
					TweenLite.to(dae10,1.5, { x:0, y:0, z:0});
					//TweenLite.to(dae10,2.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae1);
					layer.addDisplayObject3D(dae1);
					c_parent.addChild(dae9);
					layer.addDisplayObject3D(dae9);
					c_parent.addChild(dae10);
					layer.addDisplayObject3D(dae10);
				}
				switch(nameObject)
		{
		 case "duoi":
					//dae2=CustomVp.dae1;
					dae2.x=newPoint.x;
					dae2.y=newPoint.y;
					TweenLite.to(dae2,1.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae2);
					layer.addDisplayObject3D(dae2);
					break;
		 case "kieng_left":
					//dae3=CustomVp.dae1;
					dae3.x=newPoint.x;
					dae3.y=newPoint.y;
					TweenLite.to(dae3,1.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae3);
					layer.addDisplayObject3D(dae3);
					break;
		 case "kieng_right":
					//dae4=CustomVp.dae1;
					dae4.x=newPoint.x;
					dae4.y=newPoint.y;
					TweenLite.to(dae4,1.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae4);
					layer.addDisplayObject3D(dae4);
					break;
		case "banhtruoc_left":
					//dae5=CustomVp.dae1;
					dae5.x=newPoint.x;
					dae5.y=newPoint.y;
					TweenLite.to(dae5,1.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae5);
					layer.addDisplayObject3D(dae5);
					break;
		case "banhtruoc_right":
					//dae6=CustomVp.dae1;
					dae6.x=newPoint.x;
					dae6.y=newPoint.y;
					TweenLite.to(dae6,1.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae6);
					layer.addDisplayObject3D(dae6);
					break;
		case "banhsau_left":
					//dae7=CustomVp.dae1;
					dae7.x=newPoint.x;
					dae7.y=newPoint.y;
					TweenLite.to(dae7,1.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae7);
					layer.addDisplayObject3D(dae7);
					break;
		case "banhsau_right":
					//dae8=CustomVp.dae1;
					dae8.x=newPoint.x;
					dae8.y=newPoint.y;
					TweenLite.to(dae8,1.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae8);
					layer.addDisplayObject3D(dae8);
					break;
		}

				
			}
		
		public function loadDAEcustom():void{
				addEventListener(Event.ENTER_FRAME,render);
				if(nameObject=="khung"){
					dae1= CustomVp.dae1;
					dae9= CustomVp.dae3;
					dae10 = CustomVp.dae4;
					
					//set for cancel
					//dae1.x=newPoint.x;
//					dae1.y=newPoint.y;

					//set for ok
					dae1.x=0;
					dae1.z=0;
					dae1.y=500;
					TweenLite.to(dae1,1.5, { x:0, y:0, z:0});
					
					dae9.x=0;
					dae9.z=0;
					dae9.y=500;
					TweenLite.to(dae9,1.5, { x:0, y:0, z:0});
					//TweenLite.to(dae9,2.5, { x:0, y:0});
					
					dae10.x=0;
					dae10.z=0;
					dae10.y=500;
					TweenLite.to(dae10,1.5, { x:0, y:0, z:0});
					//TweenLite.to(dae10,2.5, { x:0, y:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae1);
					layer.addDisplayObject3D(dae1);
					c_parent.addChild(dae9);
					layer.addDisplayObject3D(dae9);
					c_parent.addChild(dae10);
					layer.addDisplayObject3D(dae10);
				}
				switch(nameObject)
		{
		 case "duoi":
					dae2=CustomVp.dae1;
					//set for cancel
					//dae2.x=newPoint.x;
//					dae2.y=newPoint.y;
					//set for ok
					dae2.x=0;
					dae2.z=500;
					dae2.y=0;
					TweenLite.to(dae2,1.5, { x:0, y:0, z:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae2);
					layer.addDisplayObject3D(dae2);
					break;
		 case "kieng_left":
					dae3=CustomVp.dae1;
					//set for cancel
					dae3.x=newPoint.x;
					dae3.y=newPoint.y;
					//set for ok
					dae3.x=500;
					dae3.y=0;
					dae3.z=0;
					TweenLite.to(dae3,1.5, { x:0, y:0, z:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae3);
					layer.addDisplayObject3D(dae3);
					break;
		 case "kieng_right":
					dae4=CustomVp.dae1;
					//set for cancel
					dae4.x=newPoint.x;
					dae4.y=newPoint.y;
					//set for ok
					dae4.x=-500;
					dae4.y=0;
					dae4.z=0;
					TweenLite.to(dae4,1.5, { x:0, y:0, z:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae4);
					layer.addDisplayObject3D(dae4);
					break;
		case "banhtruoc_left":
					dae5=CustomVp.dae1;
					//set for cancel
					dae5.x=newPoint.x;
					dae5.y=newPoint.y;
					//set for ok
					dae5.x=500;
					dae5.y=0;
					dae5.z=0;
					TweenLite.to(dae5,1.5, { x:0, y:0, z:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae5);
					layer.addDisplayObject3D(dae5);
					break;
		case "banhtruoc_right":
					dae6=CustomVp.dae1;
					//set for cancel
					dae6.x=newPoint.x;
					dae6.y=newPoint.y;
					//set for ok
					dae6.x=-500;
					dae6.y=0;
					dae6.z=0;
					TweenLite.to(dae6,1.5, { x:0, y:0, z:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae6);
					layer.addDisplayObject3D(dae6);
					break;
		case "banhsau_left":
					dae7=CustomVp.dae1;
					//set for cancel
					dae7.x=newPoint.x;
					dae7.y=newPoint.y;
					//set for ok
					dae7.x=500;
					dae7.y=0;
					dae7.z=0;
					TweenLite.to(dae7,1.5, { x:0, y:0, z:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae7);
					layer.addDisplayObject3D(dae7);
					break;
		case "banhsau_right":
					dae8=CustomVp.dae1;
					//set for cancel
					dae8.x=newPoint.x;
					dae8.y=newPoint.y;
					//set for ok
					dae8.x=-500;
					dae8.y=0;
					dae8.z=0;
					TweenLite.to(dae8,1.5, { x:0, y:0, z:0});
	//				dae5.x=0;
	//				dae5.y=0;
					
					//c_parent=dae5.parent;
					c_parent.addChild(dae8);
					layer.addDisplayObject3D(dae8);
					break;
		}

				
			}
		
		private function colladaLoaded(e:FileLoadEvent):void 
		{
			
			
			dae1 = object.getChildByName("khung",true);
			
			dae2 = object.getChildByName("duoi",true);
			dae3 = object.getChildByName("kieng_left",true);
			dae4 = object.getChildByName("kieng_right",true);
			dae5 = object.getChildByName("banhtruoc_left",true);
			dae6 = object.getChildByName("banhtruoc_right",true);
			//parseMesh(dae6);
			dae7 = object.getChildByName("banhsau_left",true);
			dae8 = object.getChildByName("banhsau_right",true);
			dae9 = object.getChildByName("default_matden",true);
			dae10 = object.getChildByName("default_material",true);
			var flatShadeMat:FlatShadeMaterial = new FlatShadeMaterial(light, 0xffffff,0xff0000);
			//dae10.material=flatShadeMat;
			//layer.getChildLayer(dae1, true).layerIndex=1;
//			layer.getChildLayer(dae2, true).layerIndex=2;
//			layer.getChildLayer(dae3, true).layerIndex=3;
//			layer.getChildLayer(dae4, true).layerIndex=4;
//			layer.getChildLayer(dae5, true).layerIndex=8;
//			layer.getChildLayer(dae6, true).layerIndex=5;
//			layer.getChildLayer(dae7, true).layerIndex=6;
//			layer.getChildLayer(dae8, true).layerIndex=7;
			
			
			
			layer.addDisplayObject3D(dae1);
			layer.addDisplayObject3D(dae2);
			layer.addDisplayObject3D(dae3);
			layer.addDisplayObject3D(dae4);
			layer.addDisplayObject3D(dae5);
			layer.addDisplayObject3D(dae6);
			layer.addDisplayObject3D(dae7);
			layer.addDisplayObject3D(dae8);
			layer.addDisplayObject3D(dae10);
			layer.addDisplayObject3D(dae9);
			
			
		}
		public function removeEvent():void{
			removeEventListeners(object, InteractiveScene3DEvent.OBJECT_PRESS,objectPress);
			//addEventListeners(object, InteractiveScene3DEvent.OBJECT_RELEASE,objectRe);
			removeEventListeners(object, InteractiveScene3DEvent.OBJECT_OVER,objectOver );
			removeEventListeners(object, InteractiveScene3DEvent.OBJECT_OUT,objectOut);
		}
		public function selectEvent():void{
			setInteractiveMaterials(object, true);
			addEventListeners(object, InteractiveScene3DEvent.OBJECT_PRESS,objectPress);
			//addEventListeners(object, InteractiveScene3DEvent.OBJECT_RELEASE,objectRe);
			addEventListeners(object, InteractiveScene3DEvent.OBJECT_OVER,objectOver );
			addEventListeners(object, InteractiveScene3DEvent.OBJECT_OUT,objectOut);
		}
		public function detroyEvent(index:String):void{
				if(index=="1"){
					removeZoom();
						//kamera= new InteractiveCamera3D(panorama,false);
					}
				else if(index=="2"){
						removeEventListener(Event.ENTER_FRAME, gameLoop); 
					}
			}

		public function removeDAE(targetObject:DisplayObject3D):void
		 {	
			 	//cho biet doi tuong object co trong doi tuong ma scene hien thi
				
				
				c_parent=targetObject.parent;
				c_parent.removeChild(targetObject);
				targetObject=null;
			
		}
		 public function mouseUpVp( event:MouseEvent):void
		 {
			 panorama.stage.removeEventListener( Event.ENTER_FRAME,moveHandler);	
			 panorama.stage.removeEventListener( MouseEvent.MOUSE_UP,mouseUpVp);
					if(selectedObject.x>=3||selectedObject.x<=-3){
			 		newPoint.x=selectedObject.x;
					newPoint.y=selectedObject.y;
					removeEvent();
					removeEventListener(Event.ENTER_FRAME,render);
					Main.pEffect();
					Main.txt.text="";
					if(nameObject=="khung"||nameObject=="default_material"||nameObject=="default_matden"){
						trace("loi o day");
						//huy tat ca su kien cua Object3D
						
						nameObject="khung";
						removeDAE(dae9);
						removeDAE(dae10);
						removeDAE(dae1);
					}
					else 
						removeDAE(selectedObject);
					
					
					
				
		 }
			else Main.txt.text=" You must drag Object go far away for customized";
			
			
		 }
		 public function objectPress( event:InteractiveScene3DEvent)
		 {
		 //keep reference to the current selected DisplayObject3D
		 
		// event.target.removeEventListener(InteractiveScene3DEvent.OBJECT_PRESS,objectPress);
		 selectedObject = DisplayObject3D( event.target );
		 nameObject=selectedObject.name;
		  trace("gia tri object "+nameObject);
		 //startInteraction( selectedObject);

		//  trace("enter"+layer.childLayers.length);
		  isMousePress= true;
		  initialMouseX = panorama.stage.mouseX;
          initialMouseY = panorama.stage.mouseY;
          panorama.stage.addEventListener( MouseEvent.MOUSE_UP,mouseUpVp);	
		  panorama.stage.addEventListener( Event.ENTER_FRAME,moveHandler);	
		 // scena.removeChild(dae5);
		  //layer.removeDisplayObject3D(selectedObject);
		  
		 // startInteraction(DisplayObject3D( event.target ));
		 // selectedObject.
//		  var x:Number = initialMouseX - viewport3D.stage.mouseX;
//           var y:Number = initialMouseY - viewport3D.stage.mouseY;
//           copyTransform( initialTransform );
//           moveDown( y );
//           moveRight( x );
		 }
	public function fr(evt:Event):void{
			kamera.lookAt(object);
		}

  public function objectOver( event:InteractiveScene3DEvent )
		  {
		
		// event.target.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS,objectPress);
		 panorama.containerSprite.buttonMode = true;
		  }

  public function objectOut( event:InteractiveScene3DEvent )
		  {
		   panorama.containerSprite.buttonMode = false;
		  // event.target.removeEventListener(InteractiveScene3DEvent.OBJECT_PRESS,objectPress);
		   
		  }
		private function setInteractiveMaterials(targetObject:DisplayObject3D, value:Boolean):void
{
	 if(targetObject.materials.numMaterials>0) {
                for(var i:String in targetObject.materials.materialsByName) {
                    (targetObject.materials.materialsByName[i] as MaterialObject3D).interactive = true;
                }
            } else if(targetObject.material) {
                targetObject.material.interactive = true;
            }

}		
		private function removeEventListeners(displayObject:DisplayObject3D, eventType:String, listener:Function):void{
			displayObject.removeEventListener(eventType, listener);
		 
			for each(var child:DisplayObject3D in displayObject.children) {
				removeEventListeners(child, eventType, listener);
				
			}
		}
		private function addEventListeners(displayObject:DisplayObject3D, eventType:String, listener:Function):void
		{
			displayObject.addEventListener(eventType, listener);
		 
			for each(var child:DisplayObject3D in displayObject.children) {
				addEventListeners(child, eventType, listener);
				
			}
		}

        public function objectClicked (e:InteractiveScene3DEvent):void
        {
            trace("clickedcddddddddddddddddd");
        }
        
        public function render(e:Event):void 
        {	//object.localRotationX++;
            renderer.renderScene(scena,kamera,panorama);
        }
    }
}